var e = require("../utils/prism.js");

module.exports = {
    highlight: function(r) {
        function n(e, r) {
            var n = "", a = new RegExp(r + "=['\"]?([^'\"]*)");
            return null !== e.match(a) && (n = e.match(a)[1]), n;
        }
        var a = new Array();
        a = function() {
            var r = new Array(), n = 0;
            for (var a in e.languages) "[object Function]" !== Object.prototype.toString.call(e.languages[a]) && (r[n] = a, 
            n++);
            return r;
        }();
        var l = r, t = r.match(/<\/?pre[^>]*>/g);
        if (null == t) return l;
        for (var i = [], g = 0; g < t.length; g++) 0 == g ? i.push(r.indexOf(t[g])) : i.push(r.indexOf(t[g], i[g - 1]));
        for (var u = void 0, s = 0; s < t.length - 1; ) {
            !function(e) {
                u = n(e, "class");
            }(t[s]);
            var c = t[s].match(/<*([^> ]*)/)[1];
            if (t[s + 1] === "</" + c + ">") {
                if ("pre" === c && u.split(" ").indexOf("pure-highlightjs") >= 0) {
                    var p = u.split(" ")[1];
                    if (/lang-(.*)/i.test(p) ? p = p.replace(/lang-(.*)/i, "$1") : /languages?-(.*)/i.test(p) && (p = p.replace(/languages?-(.*)/i, "$1")), 
                    -1 == a.indexOf(p) || null == p || "none" == p || "null" == p) ; else {
                        console.log("lang: " + p);
                        var o = r.substring(i[s], i[s + 1]).replace(/<pre[^>]*>/, ""), h = e.highlight(function(e) {
                            return e = e.replace(/&lt;/g, "<"), e = e.replace(/&gt;/g, ">"), e = e.replace(/&amp;/g, "&");
                        }(o), e.languages[p], p);
                        l = l.replace(o, h);
                    }
                }
                s++;
            }
            s++;
        }
        return l;
    }
};